Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

create-error

Package Overview
Dependencies
Maintainers
1
Versions
4
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

create-error

Simple helper for sub-classing the Error object

  • 0.3.1
  • latest
  • Source
  • npm
  • Socket score

Version published
Maintainers
1
Created

What is create-error?

The create-error npm package is a utility for creating custom error types in JavaScript. It simplifies the process of defining and using custom error classes, making it easier to handle specific error conditions in your applications.

What are create-error's main functionalities?

Creating a Basic Custom Error

This feature allows you to create a basic custom error type with a specific name. The custom error can then be thrown and caught like any standard error.

const createError = require('create-error');
const MyCustomError = createError('MyCustomError');

try {
  throw new MyCustomError('Something went wrong!');
} catch (err) {
  console.error(err.name); // MyCustomError
  console.error(err.message); // Something went wrong!
}

Creating a Custom Error with Additional Properties

This feature allows you to create a custom error type with additional properties. In this example, a `statusCode` property is added to the custom error.

const createError = require('create-error');
const DetailedError = createError('DetailedError', { statusCode: 400 });

try {
  throw new DetailedError('Invalid input!');
} catch (err) {
  console.error(err.name); // DetailedError
  console.error(err.message); // Invalid input!
  console.error(err.statusCode); // 400
}

Inheriting from Custom Errors

This feature allows you to create custom error types that inherit from other custom error types. This is useful for creating a hierarchy of error types in your application.

const createError = require('create-error');
const AppError = createError('AppError');
const NotFoundError = createError('NotFoundError', AppError);

try {
  throw new NotFoundError('Resource not found!');
} catch (err) {
  console.error(err.name); // NotFoundError
  console.error(err.message); // Resource not found!
  console.error(err instanceof AppError); // true
}

Other packages similar to create-error

Keywords

FAQs

Package last updated on 19 Dec 2013

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc